package com.pek.control.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pek.annontation.Permit.Permit;
import com.pek.annontation.Permit.PermitType;
import com.pek.annontation.Permit.UserPermit;
import com.pek.control.entity.ProductEntity;
import com.pek.control.service.IProductService;
import com.pek.util.BaseController;
import com.pek.util.RestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文章
 * @author 夜雨
 * @domain www.yeyv.net
 */
@RestController
@RequestMapping("/product")
public class ProductController extends BaseController<ProductEntity> {
    private IProductService productService;

    public ProductController(IProductService serviceImpl) {
        super(serviceImpl);
        this.productService = serviceImpl;
    }

    /**
     * 查询列表
     */
    @PostMapping("/list")
    public RestResult<Object> list(@RequestParam(defaultValue = "0") Integer size,
                                   @RequestBody(required = false) ProductEntity product,
                                   @RequestParam(defaultValue = "0") Integer pageSize,
                                   @RequestParam(defaultValue = "0") Integer pageNo) {
        LambdaQueryWrapper<ProductEntity> entityWrapper = new LambdaQueryWrapper<>();
        if (product != null) {
            entityWrapper.eq(product.getType() != 0, ProductEntity::getType, product.getType());
            entityWrapper.like(StringUtils.isNotEmpty(product.getTitle()), ProductEntity::getTitle, product.getTitle());
            entityWrapper.like(StringUtils.isNotEmpty(product.getForm()), ProductEntity::getForm, product.getForm());
        }
        return super.list(size, pageSize, pageNo, entityWrapper);
    }

    /**
     * 根据id获取
     */
    @GetMapping("/query")
    public RestResult<ProductEntity> query(@RequestParam String id) {
        return super.query(id);
    }

    @PostMapping("/save")
    @Permit(value = {UserPermit.INSERT}, time = 500)
    public RestResult<ProductEntity> save(@RequestBody ProductEntity product) {
        return super.save(product);
    }

    @PostMapping("/edit")
    @Permit(value = {UserPermit.UPDATE}, time = 2000)
    public RestResult<ProductEntity> edit(@RequestBody ProductEntity product) {
        return super.edit(product);
    }

    @PostMapping("/del")
    @Permit(value = {UserPermit.DELETE}, time = 2000)
    public RestResult<ProductEntity> del(@RequestParam String id) {
        return super.delete(id);
    }

    /**
     * 随机获取 {size} 个产品
     */
    @GetMapping("/random")
    public RestResult<List<ProductEntity>> random(String type,
                                                  @RequestParam(defaultValue = "3") int size) {
        var queryWrapper = new QueryWrapper<ProductEntity>();
        queryWrapper.eq(StringUtils.isNotEmpty(type), "type", type)
                .orderByDesc("rand()")
                .last("limit " + size);
        return RestResult.success(productService.list(queryWrapper));
    }

    /**
     * 随机获取{size}个产品 不在指定的列表中
     */
    @PostMapping("/randomQuery")
    public RestResult<List<ProductEntity>> random(String type,
                                                  @RequestBody List<String> notInId,
                                                  @RequestParam(defaultValue = "3") int size) {
        var queryWrapper = new QueryWrapper<ProductEntity>();
        queryWrapper.eq(StringUtils.isNotEmpty(type), "type", type)
                .notIn("id", notInId)
                .orderByDesc("rand()")
                .last("limit " + size);
        return RestResult.success(productService.list(queryWrapper));
    }
}
